package org.jeecg.jxc.base.supplier.controller;

import org.jeecg.jxc.base.supplier.entity.ServiceVO;
import org.jeecg.jxc.base.supplier.entity.Supplier;
import org.jeecg.jxc.base.supplier.service.SupplierService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 供应商管理
 * @author: 咕咚
 * @date: 2022.04.26 15:52
 * 项目名称：jeecg-boot-parent
 * 版本号：1.0
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    @GetMapping("/list")
    public ResponseEntity<Map<String,Object>> page(@RequestParam("page")Integer page,
                                                   @RequestParam("rows")Integer rows,
                                                   @RequestParam("supplierName")String supplierName){
        List<Supplier> result = supplierService.page(page, rows, supplierName);
        Integer count = supplierService.count(page, rows, supplierName);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",count);
        map.put("rows",result);
        return ResponseEntity.ok(map);
    }

    /*
    添加
     */
    @PostMapping("/save")
    public ResponseEntity<ServiceVO> insert(Supplier supplier){
        ServiceVO serviceVO = supplierService.addSupplier(supplier);
        return ResponseEntity.ok(serviceVO);
    }

    /*
    修改
    */
    @PutMapping("/save")
    public ResponseEntity<ServiceVO> update(@RequestParam("supplierId")Integer supplierId,Supplier supplier){
            supplierService.updateSupplierById(supplierId,supplier);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/delete")
    public ResponseEntity<ServiceVO> delete(@RequestParam("ids")Integer ids){
        ServiceVO serviceVO = supplierService.deleteSuplierById(ids);
        return ResponseEntity.ok(serviceVO);
    }
}
